package com.hzw.saas.api.old.storage.enums;

import java.util.Objects;

import com.hzw.saas.common.config.util.AssertUtil;


/**
 * 文件完整性状态
 *
 * @author zzl
 * @since 02/26/2021
 */
public enum FileIntegrityEnum {

    NORMAL(200, "正常"),
    UPLOADING(201, "上传中"),
    MERGING(202, "合并中"),
    ;

    private final Integer code;
    private final String text;

    FileIntegrityEnum(Integer code, String text) {
        this.code = code;
        this.text = text;
    }

    public Integer code() {
        return code;
    }

    public String text() {
        return text;
    }

    public static String text(Integer code) {
        for (FileIntegrityEnum value : FileIntegrityEnum.values()) {
            if (Objects.equals(value.code, code))
                return value.text;
        }
        return null;
    }

    public static FileIntegrityEnum instance(Integer code) {
        return instance(code, false);
    }

    public static FileIntegrityEnum instance(Integer code, boolean throwUnknown) {
        for (FileIntegrityEnum value : FileIntegrityEnum.values()) {
            if (Objects.equals(value.code, code))
                return value;
        }
        AssertUtil.assertThrow("文件完整性状态异常，请设置正确的状态码", throwUnknown);
        return null;
    }

    /**
     * 校验状态
     *
     * @param dest    校验基准
     * @param code    被校验值
     * @param eq      校验相等true或不相等false
     * @param throwEx 未通过是否抛出异常
     * @return 校验结果，true/false
     */
    public static boolean check(FileIntegrityEnum dest, Integer code, boolean eq, boolean throwEx) {
        FileIntegrityEnum instance = FileIntegrityEnum.instance(code, true);
        assert instance != null;
        boolean check = eq == (dest == instance);
        AssertUtil.assertThrow("文件状态异常：" + instance.text(), !check && throwEx);
        return check;
    }
}
